﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel.QLPhong
{
    public partial class ThongTinKhachHang : Form
    {
        private string maphong = "";
        public ThongTinKhachHang(string phong)
        {
            maphong = phong;
            InitializeComponent();
        }

        private SqlConnection con;

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ThongTinKhachHang_Load(object sender, EventArgs e)
        {

        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            if (tenKHTextBox.Text == "" || ngaySinhDateTimePicker.Text == "" || quocTichTextBox.Text == "" || cMNDTextBox.Text == "" || diaChiTextBox.Text == "")
            {
                MessageBox.Show("Nhập sai!!! Mời bạn kiểm tra lại", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string ngay = Convert.ToDateTime(ngaySinhDateTimePicker.Text).ToString("yyyy-MM-dd");
                string ngaydat = Convert.ToDateTime(DateTime.Today).ToString("yyyy-MM-dd");

                // lenh insert vao bang khach hang
                string sqlkh = "INSERT INTO KhachHang(TenKH, GioiTinh, NgaySinh, CMND, QuocTich, DiaChi, SoDT, MaPhong, DaXoa) VALUES(N'"+
                    tenKHTextBox.Text + "', N'" + gioiTinhComboBox.Text + "', '" + ngay + "', '" + cMNDTextBox.Text + "', N'" +
                    quocTichTextBox.Text+"', N'"+diaChiTextBox.Text+"', '"+soDTTextBox.Text+"', '"+maphong+"', '0')";
                serverdata(sqlkh);
                
                // lenh insert vao bang phong luu tru
                string sqlplt = "INSERT INTO PhongLuuTru(MaPhong, MaKH, NgayDat, TienDatTruoc, DaXoa) VALUES('" +
                    maphong + "', (SELECT TOP 1 MaKH FROM KhachHang ORDER BY MaKH DESC),'" + ngaydat + "', '" + tbtiendat.Text + "', '0')";
                serverdata(sqlplt);

                MessageBox.Show("Đã thuê phòng");
                this.Dispose();
            }
        }

        public void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void cMNDTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8);
        }

        private void soDTTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8);
        }

        private void tbtiendat_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8);
        }
    }
}
